package com.pengtu.gsj.service.impl.check.gcjs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.check.gcjs.FieldOperationDao;
import com.pengtu.gsj.dao.check.gcjs.StandardManagementDao;
import com.pengtu.gsj.dao.system.AttachmentDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.gcjs.FieldOperation;
import com.pengtu.gsj.entity.gcjs.StandardManagement;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.gcjs.ConOpearAreaService;
import com.pengtu.gsj.service.check.gcjs.RuleManagerService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class ConOpearAreaServiceImpl implements ConOpearAreaService{

	@Autowired
	private AttachmentDao attachmentDao;
	@Autowired
	private FieldOperationDao fieldOperationDao;
	@Autowired
	private CheckTableService checkTableService;
	@Autowired
	private StandardManagementDao standardManagementDao;
	@Autowired
	private SystemManageService systemManageService;
	@Autowired
	private RuleManagerService ruleManagerService;
	
	@Override
	@Transactional(readOnly = true)
	public List<Attachment> getAttachmentList(QueryFilter queryFilter) {
		return attachmentDao.find(queryFilter);
	}

	@Override
	public Map<String, Object> getConOpearAreas(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Object> values = new ArrayList<Object>();
		List<String> resultLists = new ArrayList<String>();
		List<Double> scoreLists = new ArrayList<Double>();
		List<StandardManagement> standLists = new ArrayList<StandardManagement>();
		List<Attachment> list = new ArrayList<Attachment>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String) jsonObject.get("yearFull"));
		String itemNameIds = (String)jsonObject.get("itemProjectId");
		//交通保障方案的有无标志
		String opearRuleFlag = "0";
		//作业区布施设施的有无标志
		String supportPlanFlag = "0";
		double total =0; //检查得分
		double percentage =0; //系统完善度
		double tableFull = yearFull * 4;//表总分
		
		//根据tag标签和时间、标签查询是否有附件
		list = systemManageService.getCheckAttachments(startTime, endTime, tag);
		if (StringUtils.isNotEmpty(itemNameIds)) {
			//当检索条件不为空的时候更新分数
			values.add(itemNameIds);
			//通过逗号将每个id切割出来，并搜索算分
			String[] idArrs = itemNameIds.split(",");
			for (int i = 0; i < idArrs.length; i++) {
				//设置临时变量保存分数
				double temp = 0;
				//根据tag检索是否有附件
				if( i > 3) {
					break;
				}
				StandardManagement stManagement = standardManagementDao.get(idArrs[i]);
				standLists.add(stManagement);
				List<Attachment> attachments = stManagement.getAttachments();
				//每次遍历之前都需要重置这两个属性
				opearRuleFlag = "0";
				supportPlanFlag = "0";
				for (Attachment attachment : attachments) {
					//巡查该对象里面是否包含 交通保障方案（tag="sgzy_jtbzfa"）的附件
					if ("sgzy_jtbzfa".equals(attachment.getTag()) && opearRuleFlag == "0"){
						temp = yearFull;
						opearRuleFlag = "1";
						supportPlanFlag = "1";
					}
				}
				total = MathsUtils.sumDouble(total, temp);
				scoreLists.add(temp);
				resultLists.add(opearRuleFlag);
				resultLists.add(supportPlanFlag);
			}
			percentage = MathsUtils.divDouble(total * 100, tableFull, 4);
			map.put("percent", percentage);
			map.put("resultLists", resultLists);
			map.put("scoreLists", scoreLists);
			//添加一个标记，确定前端是否输出表格和在空条件检索并导出时是否输出表头
			map.put("flag",true);
		}
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);
		map.put("att", list);
		map.put("manageData", standLists);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}

	@Override
	public List<FieldOperation> getConOpearAreaProject() {
		return fieldOperationDao.getConOpearAreaProject();
	}
}
